﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
using UDS.Components;

namespace UDS.SubModule.Role
{
    /// <summary>
    /// ListView 的摘要说明。
    /// </summary>
    public partial class ListViewPage : Page
    {
        protected int DisplayType
        {
            get;
            private set;
        }

        protected int RoleID
        {
            get;
            private set;
        }

        protected bool RefreshRoleList
        {
            get;
            private set;
        }

        protected override void OnInit(EventArgs e)
        {
            cmdDeleteStaffFromRole.Click += cmdDeleteStaffFromRole_Click;
            cmdAddToRole.Click += cmdAddToRole_Click;
            base.OnInit(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            RoleID = Int32.Parse((Request.QueryString["Role_ID"] == null) ? "0" : Request.QueryString["Role_ID"]);
            DisplayType = Int32.Parse((Request.QueryString["DisplayType"] == null) ? "0" : Request.QueryString["DisplayType"]);

            if (!Page.IsPostBack)
            {
                if (DisplayType == 0)
                {
                    cmdAddToRole.Visible = false;
                    cmdDeleteStaffFromRole.Visible = true;
                }
                else
                {
                    cmdAddToRole.Visible = true;
                    cmdDeleteStaffFromRole.Visible = false;
                }

                RefreshRoleList = (Request.QueryString["Refresh"] == null) ? false : true;

                BindGrid();
                cmdAddToRole.Attributes.Add("OnClick", "javascript:return confirm('是否把选定人员加入到角色！');");
                cmdDeleteStaffFromRole.Attributes.Add("OnClick", "javascript:return confirm('是否把选定人员从角色脱离！');");
            }

            base.OnLoad(e);
        }

        public string GetSelectImage(string NormalImg, string SelectedImg, int selected, int position)
        {
            if (selected == position)
            {
                return SelectedImg;
            }
            return NormalImg;
        }

        private void BindGrid()
        {
            SqlDataReader dr; //存放人物的数据
            var db = new Database();
            var prams = new[]
            {
                Database.MakeInParam("@RoleID", SqlDbType.Int, 4, RoleID)
            };
            if (DisplayType == 0)
            {
                dr = db.RunDataReader("dbo.sproc_GetStaffInRole", prams);
            }
            else
            {
                dr = db.RunDataReader("dbo.sproc_GetStaffNotInRole", prams);
            }

            DataTable dt;
            using (dr)
            {
                dt = Tools.ConvertDataReaderToDataTable(dr);
            }

            dbStaffList.DataSource = dt.DefaultView;
            dbStaffList.DataBind();
        }

        private string GetSelectedItemID(string controlID)
        {
            string selectedID = "";
            //遍历DataGrid获得checked的ID
            foreach (DataGridItem item in dbStaffList.Items)
            {
                if (((CheckBox) item.FindControl(controlID)).Checked)
                {
                    selectedID += dbStaffList.DataKeys[item.ItemIndex] + ",";
                }
            }
            if (selectedID.Length > 0)
            {
                selectedID = selectedID.Substring(0, selectedID.Length - 1);
            }
            return selectedID;
        }

        private void cmdDeleteStaffFromRole_Click(object sender, EventArgs e)
        {
            string selectID = GetSelectedItemID("Staff_ID");
            if (selectID.Trim() != "")
            {
                if (Components.Role.DelStaffFromRole(RoleID, selectID) != 0)
                {
                    Logger.Log("脱离角色出错！");
                    Response.Redirect("../Error.aspx");
                }
                else
                {
                    Response.Redirect("ListView.aspx?Role_ID=" + RoleID + "&DisplayType=" + DisplayType);
                }
            }
            else
            {
                Response.Write("<script language='javascript'>alert('请选择要脱离的人员');</script>");
            }
        }

        private void cmdAddToRole_Click(object sender, EventArgs e)
        {
            string selectID = GetSelectedItemID("Staff_ID");
            if (selectID.Trim() != "")
            {
                if (Components.Role.AddStaffFromRole(RoleID, selectID) != 0)
                {
                    Logger.Log("加入角色出错！");
                    Response.Redirect("../Error.aspx");
                }
                else
                {
                    Response.Redirect("ListView.aspx?Role_ID=" + RoleID + "&DisplayType=" + DisplayType);
                }
            }
            else
            {
                Response.Write("<script language='javascript'>alert('请选择要脱离的人员');</script>");
            }
        }

        #region 翻页事件

        public void DataGrid_PageChanged(object sender, DataGridPageChangedEventArgs e)
        {
            dbStaffList.CurrentPageIndex = e.NewPageIndex;
            BindGrid();
        }

        #endregion
    }
}